using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;

namespace MemoryCacheWebDemo.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }

        [HttpGet]
        public List<int> GetTest() 
        {
            Random random = new Random();
            var momoryCache = Request.HttpContext.RequestServices.GetService<IMemoryCache>();
            var key = $"OwnerOrgCode:{1111}";
            var result = momoryCache.GetOrCreate(key, cacheEntry => 
            {
                // cacheEntry.SlidingExpiration = TimeSpan.FromSeconds(10);
                cacheEntry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(10);
                return new List<int> { random.Next(0, 100), random.Next(0, 100) };
            });
            return result;
        }

        public void TestSwitch() 
        {
            int provinceValue = 0;
            int cityValue = 0;
            int areaValue = 0;
            bool hasTotalTag = false;
            List<int> datas = new List<int>();

            datas = (provinceValue, cityValue, areaValue, hasTotalTag) switch
            {
                var (p, c, a, h) when p > 0 && c > 0 && a > 0 && h => [],
                _ => []
            };
        }
    }
}
